package com.whh.cos.utils;

import com.qcloud.cos.COS;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;

import java.util.stream.StreamSupport;

/**
 * @author huahui.wu.
 *         Created on 2018/1/16.
 */
public class CosAuth {
    /**
     * accressKey
     */
    private  String accressKey;
    /**
     * secretKey
     */
    private  String secretKey;
    /**
     * 地区
     */
    private  String region;

    private CosAuth(){
        this.accressKey = "AKIDkLkP0QkgV82lIdvQ6cw3yhG4kEWfHMuG";
        this.secretKey = "IONBrLgpnaAbqtoX4PZYL9C5ymOr9Kk3";
        this.region = "ap-shanghai";
    }

    private static COSClient cosClient = null;

    public static COSClient getCosAuth() {
        if (cosClient == null) {
            synchronized (CosAuth.class) {
                if (cosClient == null) {
                    CosAuth cosAuth = new CosAuth();
                    COSCredentials cosCredentials = new BasicCOSCredentials(cosAuth.accressKey,
                            cosAuth.secretKey);
                    ClientConfig config = new ClientConfig(new Region(cosAuth.region));
                    cosClient = new COSClient(cosCredentials, config);
                }
            }
        }
        return cosClient;
    }

    public static void closeCosAuth() {
        if (cosClient != null) {
            cosClient.shutdown();
        }
    }
}
